<?php

namespace app\servergoods\traits;

trait OperateAppCate
{
	/**
     * @author:      李颖
     * @DateTime:    2017-09-29 14:05:25
     * @Description: 操作分类 该分类的结构： app_id other_app_read is_public pid
     * @Description: 该种分类相似的不少 避免在逻辑层粘贴复制大量代码 故写为traits
     */
    

	/*获取某个分类详情 不删除的便可*/
    private function _oneCateInfo(array $data, $Model, array $otherMap = []): array 
    {
        $_where['id'] = $data['id'];
        $_where['status'] = isset($otherMap['status']) ? $otherMap['status'] : ['neq',-1];

        if(isset($data['app_id'])){   
            $manageAppId = \app\manage\service\ApiBase::getAppId();
            $_whereOr = $manageAppId == $data['app_id'] ? true : '`app_id`=' . $data['app_id'] . ' OR FIND_IN_SET(' . $data['app_id'] . ',`other_app_read`) OR is_public = 1';
        }else{
            $_whereOr = true;
        }
        $res = $Model->where($_where)->where($_whereOr)->find();
        return auto_return($res);
    }

    /*后台删除某个分类*/
    private function _delOneCate(array $data, $Model): array 
    {
        //判断是否存在子类
        $_findChild['pid'] = $data['id'];
        $_findChild['status'] = ['neq', '-1'];
        $manageAppId = \app\manage\service\ApiBase::getAppId();
        $_findChildOr = $manageAppId == $data['app_id'] ? true : '`app_id`=' . $data['app_id'] . ' OR FIND_IN_SET(' . $data['app_id'] . ',`other_app_read`) OR is_public = 1';
        $childList = $Model->where($_findChild)->where($_findChildOr)->select();
        if($childList){
            return verify_err($childList, '该分类下存在子类[' . $childList[0]['name'] . ']，不可删除');
        }

        $_where['id'] = $data['id'];
        $_where['status'] = ['neq', '-1'];
        $data['app_id'] != $manageAppId && $_where['app_id'] = $data['app_id'];
        $res = $Model->where($_where)->update(['status'=>'-1']);
        return $res ? auto_success($res, '成功删除该分类') : verify_err($res, '此分类已删除或不属于该应用');
    }
}